<template>
	<view class="pages">
		<navBar title="成绩查询" bgColor="#156ed2" @getHeadHeight="getHeadHeight" :backIcon="false"
			path='/pages/search/search' />
		<view class="banner">
			<image src="../../static/images/check-index.png" style="width: 100%;" mode="widthFix"></image>
		</view>
		<view class="search-form">
			<u-card :border="false" :show-head="false">
				<view class="" slot="body">
					<u-form :model="form" ref="uForm">
						<u-form-item label="考试" prop="form.exam_id" borderBottom @click="show = true;">
							<picker @change="gChange" :value="exam_list[indexg].id" :range="exam_list" range-key="name">
								<view style="padding: 20rpx;background-color: white;">{{ selectExam[0].name }}</view>
							</picker>
						</u-form-item>
					</u-form>
					<view class="mt-5">
						<u-button type="primary" @click="submit">查 询</u-button>
					</view>
				</view>
			</u-card>

		</view>
		<view v-if="showResult" class="result">
			<u-card :border="false" :show-head="false">
				<view class="" slot="body">
					<u-table>
						<u-tr>
							<u-td>{{searchRes[0].user.username}}</u-td>
							<u-td>{{searchRes[0].class.name}}</u-td>
						</u-tr>
						<u-tr>
							<u-th>学科</u-th>
							<u-th>成绩</u-th>
							<u-th>试卷</u-th>
							<u-th>点评</u-th>
						</u-tr>
						<u-tr v-for="(item, index) in searchRes" :key="index">
							<u-td>{{item.branch.name}}</u-td>
							<u-td>{{item.score}}</u-td>
							<u-td>
								<view class="d-block" @click="showDetail(item.id)">查看</view>
							</u-td>
							<u-td>
								<view class="" @click="showComment(item.comment)">评语</view>
							</u-td>
						</u-tr>
						<u-tr v-if="showImage">
							<u-td>
								<view class="d-block">
								<image @click="showBigImage()" :src="image" mode="widthFix" style="width: 100%;"></image>
								</view>
							</u-td>
						</u-tr>
						<u-popup v-model="showpop" mode="center" border-radius="14" width="80%" height="160px;">
							<view class="p-2">{{commentpop}}</view>
						</u-popup>
					</u-table>
				</view>
			</u-card>
		</view>
		<BottomBar :selected='selected'></BottomBar>
	</view>
</template>

<script>
	import $G from '../../static/common/global.js';
	import $C from '../../static/common/config.js';
	import $H from '../../static/common/request.js';
	import {
		mapState
	} from 'vuex';
	import {
		createLogger
	} from 'vuex';
	export default {
		data() {
			return {
				form: {
					username: '',
					branch_id: 0,
					exam_id: 0
				},
				branch_list: [],
				exam_list: [],
				showb: false,
				showg: false,
				bselected: '请选择学科',
				gselected: '请选择考试项目',
				searchRes: [],
				indexb: 0,
				indexg: 0,
				showpop: false,
				commentpop: '',
				HeadNavHeight: 0,
				selected: 1,
				showResult: false,
				showImage: false,
				image:''
			}
		},
		created() {
			this.getExam();
			this.getBranch();
		},
		onShow() {
			this.$store.dispatch('getUserInfo');
			this.form.username = this.userInfo.username ? this.userInfo.username : ''
		},
		computed: {
			selectExam() {
				return this.exam_list.filter(exam => exam.id === this.form.exam_id);
			},
			...mapState({
				userInfo: state => state.user
			})
		},
		methods: {
			getBranch() {
				$H.get('/branch/list', {}, {}).then(res => {
					this.branch_list = res.data
					console.log(this.branch_list)
				});
			},
			getExam() {
				$H.get('/exam/list', {}, {}).then(res => {
					this.exam_list = res.data
					this.form.exam_id = this.exam_list[0].id
					console.log(this.exam_list)
				});
			},
			bChange: function(e) {
				this.indexb = e.detail.value;
				this.form.branch_id = this.branch_list[this.indexb].id
			},
			gChange: function(e) {
				this.indexg = e.detail.value;
				this.form.exam_id = this.exam_list[this.indexg].id
			},
			showDetail(id){
				uni.navigateTo({
					url:'/pages/score/score?id=' + id
				})
			},
			showComment(comment) {
				console.log(comment);
				this.commentpop = comment;
				this.showpop = true
			},
			showBigImage(){
				uni.previewImage({
					urls:[this.image],
					current:this.image
				})
			},
			submit() {
				$H.post('/score/search', {
					data: this.form
				}, {
					token: true
				}).then((res) => {
					console.log(res);
					res.data.forEach(item => {
						item.image = $C.imageUrl + item.image;
					})
					console.log(res.data);
					this.searchRes = res.data
					this.showResult = true
					console.log(this.searchRes);
				}).catch((error) => {
					// 请求失败，将错误信息传递给reject
					createLogger(error);
				});
			},
			// 导航栏高度(单位px)
			getHeadHeight(e) {
				this.HeadNavHeight = e
				console.log(this.HeadNavHeight);
			}
		}
	}
</script>

<style lang="scss">
	.u-page {
		padding: 0;
	}
</style>